package com.eshop.modules.shop.service.impl;

import java.lang.invoke.SerializedLambda;
import java.io.IOException;
import java.util.Iterator;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.shop.service.dto.SystemConfigDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.shop.service.dto.SystemConfigQueryCriteria;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.util.StrUtil;
import com.eshop.utils.RedisUtils;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.shop.service.SystemConfigService;
import com.eshop.modules.shop.domain.SystemConfig;
import com.eshop.modules.shop.service.mapper.SystemConfigMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class SystemConfigServiceImpl extends BaseServiceImpl<SystemConfigMapper, SystemConfig> implements SystemConfigService
{
    private final IGenerator generator;
    private final RedisUtils redisUtils;
    
    public String getData(final String name) {
        final String result = this.redisUtils.getY(name);
        if (StrUtil.isNotBlank((CharSequence)result)) {
            return result;
        }
        final LambdaQueryWrapper<SystemConfig> wrapper = (LambdaQueryWrapper<SystemConfig>)new LambdaQueryWrapper();
        wrapper.eq(SystemConfig::getMenuName, (Object)name);
        final SystemConfig systemConfig = (SystemConfig)((SystemConfigMapper)this.baseMapper).selectOne((Wrapper)wrapper);
        if (systemConfig == null) {
            return "";
        }
        return systemConfig.getValue();
    }
    
    public Map<String, Object> queryAll(final SystemConfigQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<SystemConfig> page = (PageInfo<SystemConfig>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)SystemConfigDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<SystemConfig> queryAll(final SystemConfigQueryCriteria criteria) {
        return (List<SystemConfig>)((SystemConfigMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate((Object)SystemConfig.class, (Object)criteria));
    }
    
    public void download(final List<SystemConfigDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final SystemConfigDto yxSystemConfig : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("字段名称", yxSystemConfig.getMenuName());
            map.put("默认值", yxSystemConfig.getValue());
            map.put("排序", yxSystemConfig.getSort());
            map.put("是否隐藏", yxSystemConfig.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public SystemConfig findByKey(final String key) {
        return (SystemConfig)this.getOne(Wrappers.<SystemConfig>lambdaQuery().eq(SystemConfig::getMenuName, (Object)key));
    }
    
    public SystemConfigServiceImpl(final IGenerator generator, final RedisUtils redisUtils) {
        this.generator = generator;
        this.redisUtils = redisUtils;
    }
}
